from wormbane.ui.selection_dialog import selection_dialog

import random

def command_eat(game, ui):
    eatable = [thing for thing in game.hero.inv if thing.is_food]
    if not eatable:
        game.write(game.hero, 'has nothing to eat')
        return

    if len(eatable) == 1:
        selection = set(eatable)
    else:
        selection = selection_dialog(ui.stdscr, list(reversed(eatable)),
                                     prompt = 'Eat')
        if not selection:
            game.write('Never mind.')
            return

    item = random.choice(list(selection))
    game.write(game.hero, 'eats', item)
    item.move_to(None)
    if item.is_mushroom:
        if not game.hero.hallucinating:
            game.write('Far out, %s!'
                       % ('babe' if game.hero.gender == 'female' else 'dude'))
        game.hero.hallucinating = (max(game.turn, game.hero.hallucinating)
                                   + random.randint(50, 100))
